Skip to content

Persist prepared bindings and assemble apps with them - #2

Draft
noise64 wants to merge 11 commits into
runtime-baselinefrom
prepared-assets
Draft

noise64 wants to merge 11 commits into
runtime-baselinefrom
prepared-assets

Conversation

@noise64

@noise64 noise64 commented Nov 16, 2026 •

Copy link
Copy Markdown

Touches GOL-1850 (PY-02): the separate prepare and assemble steps that Golem's Python build uses. It does not resolve the issue.

Persisted prepare/assemble API for the Golem Python SDK. Stacked on #1 (runtime-baseline).

  • Bind, link and initialize are separate steps. ComponentGenerator::generate binds the WIT world, links the libraries and initializes the result. Initialization follows an InitPlan: owned import stubs instead of the borrowed Resolve, the environment, the preopens, and the entropy derived from the linked component, the app name and the Python path files. The Python path search sorts directory entries, so the library order does not depend on the file system.
  • Prepared assets. BindingsPreparer::prepare resolves a WIT world once and returns Prepared: the bindings module, the generated Python bindings, the runtime libraries, the WASI adapter, the standard library, and the symbols and import stubs for initialization. Prepared::write stores content-addressed blobs and a manifest. The SHA-256 of the manifest is the identity. Prepared::read rejects a changed or missing blob, an extra file, a non-canonical manifest, another format and another initialization WIT. The initialization WIT digest normalizes CRLF, so a checkout that converts line endings builds a matching assembler. Unpacking a blob accepts only regular files and directories, so a link in a blob cannot point the build-time mtime update at host files.
  • Assembly. Assembler::assemble links the prepared assets with an app and its native extensions, without WIT processing or code generation. Each Python path entry has a mount name, and the library name of an extension is /{name}/{path}, so the names do not depend on the order of the entries. An app that carries a componentize-py.toml file is rejected, because it would change the bindings.
  • Caller-supplied store and linker. Assembly::initialize takes the caller's Store and Linker, built on the caller's engine with its own WASI implementation, as InitPlan describes: environment, preopens, entropy and build clocks. add_trapping_stubs adds the plan's import stubs to any linker. The crate does not choose engine settings or WASI for the caller. componentize and assemble keep this crate's WASI setup and default_engine. Wasmtime moves to 50.0.1, the version that Golem's engine uses; this change alone needs no source change.
  • Assembler without the runtime. The embedded-runtime feature, on by default, builds CPython and the runtime into the crate. Without the feature, the crate reads and assembles prepared assets, which carry their own runtime, does not build CPython and does not depend on wasmtime-wasi. Preparing and componentize are then not compiled. The binary requires the feature.
  • CLI. componentize-py prepare <dir> prints the identity. componentize-py assemble --prepared <dir> [--expect-identity <id>] <app> -p [name=]dir links and initializes.

Tests in tests/componentize.rs prepare once and assemble two unrelated apps in separate processes, check that the prepared files do not change, and check that rejected assets do not import the app: the app writes a marker on import, and the marker appears only for intact assets.

Known gaps: the embedded-runtime check at the start of bind cannot fail, because bind exists only with the feature. The initialize_component docs do not say that the caller's engine needs wasm_component_model_map. No test covers support-library linking with prepared assets, finalization of exported resources, a non-canonical manifest, a missing blob or another format; the digest-mismatch test corrupts the first blob that read_dir returns.

Not solved here: two assemblies of the same input can still differ in a few bytes. The difference follows host metadata of the build directories that assembly creates.

Verification

On this head, cad25ee (Linux x86_64, local, with the CPython build from the cache cases of #1). This head merges #1's head 7389c59, which takes wit-component from the wasm-tools fork. The merge brings the same change as #1 and no other change.

  • cargo test --release -- --skip http_p3_example: passed (119 lib, 21 bindings, 12 componentize tests). prepared::tests::unpack_rejects_links covers symlink and hard-link entries.
  • bash .github/workflows/lint.sh: passed.
  • Two componentize-py prepare runs with the binary from this head gave identical files and the identity a120875a…0234. The previous identity, b1d3997f…, differs only in the blob of libcomponentize_py_runtime.so (20 bytes longer). Its strings differ only in .llvm.<n> symbol suffixes. The wit-component package changed from a path source to a git source, so the build hashes of the wit-bindgen crates and the runtime crate change. This is the probable cause of the suffix changes. No rebuild proved it.
  • Not run: http_p3_example, because its port is taken on the build machine.
  • CI: the Test workflow runs only for PRs into main, so this stacked PR has no checks. A missing result is not a pass.
  • Not covered: byte-identical assembly (above), and Wasm exceptions on Golem's shared engine.

ComponentGenerator::generate now binds the WIT world, links the libraries and
initializes the result as separate steps. Initialization follows an InitPlan:
owned import stubs instead of the borrowed Resolve, the environment,
preopens and entropy. The linker takes the adapter as input, and the Python
path search sorts directory entries so that the library order does not
depend on the file system.
BindingsPreparer resolves a WIT world once and returns Prepared assets: the
bindings module, the generated Python bindings, the runtime libraries, the
standard library, and the symbols and import stubs that initialization needs.
Prepared::write stores them as content-addressed blobs and a manifest whose
digest is their identity. Prepared::read verifies every blob, the manifest
form and the initialization WIT before use.

Assembler links prepared assets with an app and its native extensions
without WIT processing. Each Python path entry has a mount name, so the
library names of extensions do not depend on the order of the entries. An
app that carries a componentize-py.toml file is rejected, because it would
change the bindings. Assembly::initialize_with lets the caller supply the
engine that runs initialization.

The prepare and assemble subcommands expose this from the command line.
Golem's engine uses Wasmtime 50.0.1, so the crate can run initialization on
an engine that Golem configures.
Assembly::initialize takes an Initializer: the engine, an optional linker
setup and an optional store setup, such as fuel or an epoch deadline that the
engine requires. componentize and assemble use default_engine.

The embedded-runtime feature, on by default, builds CPython and the runtime
into the crate. Without it the crate reads and assembles prepared assets, which
carry their own runtime, and preparing or componentize fail with a clear
error. The binary requires the feature.
Golem's Wasmtime has its own WASI implementation, so the crate cannot build the
WASI context for it. Assembly::initialize now takes the caller's store and
linker, and add_trapping_stubs adds the plan's import stubs to any linker.
initialize_component runs initialization for either. Without the
embedded-runtime feature the crate does not depend on wasmtime-wasi, and
Assembly::initialize_default keeps the crate's own WASI setup for the CLI.

Allow the deprecated filetime::set_file_times call: newer filetime releases
deprecate it, and the std replacement does not set directory times on every
platform.
Without the embedded-runtime feature the crate only reads, assembles and
initializes prepared assets, so the WIT generator, registry client, CLI and
async runtime become optional dependencies of that feature. An embedder that
links the assembler then pulls in fewer crates.
Unpacking a prepared blob created any symlink or hard link in it, and
setting the build mtimes then followed the link to host files.
Preparation writes only files and directories, so unpacking now rejects
every other entry.

The digest of the initialization WIT now normalizes CRLF, so assets
prepared on one platform match an assembler built from a checkout that
converted line endings.

InitPlan::new takes the build entropy from its caller.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant